/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.smg.community.service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.smg.community.dao.CrmUserDao;
import com.smg.community.model.CrmRole;
import com.smg.community.model.CrmUser;
import com.smg.community.vo.query.CrmRoleQuery;
import com.smg.community.vo.query.CrmUserQuery;

import cn.org.rapid_framework.page.Page;


/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

@Service
public class CrmUserManager extends BaseManager<CrmUser,java.lang.Integer>{

	private CrmUserDao crmUserDao;
	
	/**
	 *  缓存用户信息,避免每次都访问数据库
	 */
	private static  ConcurrentHashMap<String, CrmUser> userCacheMap = new  ConcurrentHashMap<String, CrmUser>(600);
	
	public static double count = 0;
	@Autowired 
	private CrmRoleManager crmRoleManager;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setCrmUserDao(CrmUserDao dao) {
		this.crmUserDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.crmUserDao;
	}
	
	
	@Override
	public CrmUser getById(Integer id) throws DataAccessException {
		CrmUser user =  userCacheMap.get(String.valueOf(id));
		if ( user == null){
			user = super.getById(id);
			if(user == null){
				System.out.println("userid ="+ id +" not exists");
				this.removeById(id);
				return null;
			}
			userCacheMap.put(String.valueOf(id), user);
		}	else{
			count ++;
		}
		return user;
	}
	/**
	 * 根据用户名密码获取用户
	 * */
	public CrmUser getCrmUserByNameAndPwd(String userName,String userPass){
		return crmUserDao.getCrmUserByNameAndPwd(userName, userPass);
	}
	/**
	 * 根据查询条件获取用户列表
	 * */
	public List<CrmUser> getBy(CrmUserQuery query){
		return crmUserDao.getBy(query);
	}
	/**
	 * 批量删除用户
	 * */
	public int deleteByIds(String ids){
		return crmUserDao.deleteByIds(ids);
	}
	/**
	 * 批量更新用户为离职状态
	 * */
	public int updateByIds(String ids){
		return crmUserDao.updateByIds(ids);
	}
	
	/**
	 * 根据用户id 更新用户角色
	 * */
	public int updateRoleIdsById(String roleIds,String id) {
		return crmUserDao.updateRoleIdsById(roleIds, id);
	}
	public Page findPage(CrmUserQuery query) {
		return crmUserDao.findPage(query);
	}
	public List<CrmUser> getUserListByCityno(int cityno) {
		return crmUserDao.getUserListByCityno(cityno);
	}
	/**
	 * 获取1个城市的所有BD BD经理 BD主管 BD助理 List
	 * @param cityno
	 * @return
	 */
	public List<CrmUser> getBdListByCityno(int cityno){
		List<CrmUser> userList =getUserListByCityno(cityno);
		List<CrmUser> bdList = new ArrayList<CrmUser>();
		for(CrmUser user:userList){
			if(crmRoleManager.isBdManager(user.getRoleIds()) || crmRoleManager.isBdMaster(user.getRoleIds()) || crmRoleManager.isBd(user.getRoleIds()) || crmRoleManager.isBdAssistant(user.getRoleIds())){
				bdList.add(user);
			}
		}
		return bdList;
		
	}	
	
	/**
	 * 获取1个城市的所有BD BD经理 BD主管 BD助理 的id字符串
	 * @param cityno
	 * @return
	 */
	public String getBdOrBdMasterUserIdsByCityno(int cityno){
		String userids = "";
		List<CrmUser> bdList =getUserListByCityno(cityno);
		if(bdList.size()>0){
			for(int index=0;index<bdList.size();index++){
				CrmUser user = bdList.get(index);
				if(crmRoleManager.isBdManager(user.getRoleIds()) || crmRoleManager.isBdMaster(user.getRoleIds()) || crmRoleManager.isBd(user.getRoleIds()) || crmRoleManager.isBdAssistant(user.getRoleIds())){
					if(index == bdList.size()-1){
						userids = userids+bdList.get(index).getId();
					}else{
						userids = userids+bdList.get(index).getId()+",";
					}
				}
			}	
		}else{
			return null;
		}
		return  userids;
	}
 	/**
	 * 根据城市和角色名称返回列表
	 * @return
	 */
	public List<CrmUser> getUserListByRoleNameAndCityNo(String rolename,int cityno){
		CrmRoleQuery query = new CrmRoleQuery();
		query.setName(rolename);
		CrmRole role = crmRoleManager.getRoleByRoleQuery(query);
		if(role == null)
			return  null;
		CrmUserQuery userQuery = new CrmUserQuery();
		userQuery.setRoleIds(String.valueOf(role.getId()));
		userQuery.setCityNo(String.valueOf(cityno));
		userQuery.setState(1);
		List<CrmUser> crmUserList = this.getBy(userQuery);
		if(crmUserList == null)
			return null;
		return crmUserList;
	}
	/**
	 * 根据角色名称返回列表
	 * @return
	 */
	public List<CrmUser> getUserListByRoleName(String rolename){
		CrmRoleQuery query = new CrmRoleQuery();
		query.setName(rolename);
		CrmRole role = crmRoleManager.getRoleByRoleQuery(query);
		if(role == null)
			return  null;
		CrmUserQuery userQuery = new CrmUserQuery();
		userQuery.setRoleIds(String.valueOf(role.getId()));
		userQuery.setState(1);
		List<CrmUser> crmUserList = this.getBy(userQuery);
		if(crmUserList == null)
			return null;
		return crmUserList;
	}
	/**
	 * 根据角色id返回列表
	 * @return
	 */
	public List<CrmUser> getUserListByRoleid(String roleid){
		CrmUserQuery userQuery = new CrmUserQuery();
		userQuery.setRoleIds(roleid);
		userQuery.setState(1);
		List<CrmUser> crmUserList = this.getBy(userQuery);
		if(crmUserList == null)
			return null;
		return crmUserList;
	}
	/**
	 * 清空hashMap
	 */
	public static void refreshCach() {
		userCacheMap = new ConcurrentHashMap<String, CrmUser>();
	}
	
	/**
	 * 如果某一个用户的信息变动过 则应该移除掉这个用户的缓存重新去读取数据库
	 * @param userid
	 */
	public void removeFromCach(String userid){
		userCacheMap.remove(userid);
	}
}
